/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;


import com.zjdt.epserver.db.dao.LocationDao;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class LocationsDaoImpl implements LocationDao{

    static Logger logger = Logger.getLogger(LocationsDaoImpl.class);
    private final JdbcTemplate jdbcTemplate;

    public LocationsDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }   
    
    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into locations(driverID,tripID,time,longitude,latitude) values(?,?,?,?,?)";
        logger.debug(sql+"?"+bean);
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doCreateBatch(List<Object[]> beans) {
        boolean flag = false;
         String sql = "insert into locations(driverID,tripID,time,longitude,latitude) values(?,?,?,?,?)";
        logger.debug(sql);
        int[] rows = jdbcTemplate.batchUpdate(sql, beans);
        flag = true;
        return flag;
    }

    @Override
    public List<Map<String, Object>> findAll(String time, int driverID, int tripID) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public Map<String, Object> findByRID(String rid) {
        String sql = null;
        try{
            sql = "select * from  location where rid = ?";
            return jdbcTemplate.queryForMap(sql,rid);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }
        
    }
    
}
